
package com.bear.cmcc.NetTestHTTP.ui.adapter;

import com.bear.cmcc.NetTestHTTP.R;
import com.bear.cmcc.NetTestHTTP.component.HttpTestRoundInfoBean;
import com.bear.cmcc.NetTestHTTP.component.NetTestRoundInfoBean;
import com.bear.cmcc.NetTestHTTP.component.NetTestSpeedBean;
import com.bear.cmcc.NetTestHTTP.constant.MyCommonConstant;
import com.bear.cmcc.NetTestHTTP.ui.activity.HttpTestActivity;
import com.bear.cmcc.NetTestHTTP.ui.activity.NetTestActivity;
import com.bear.cmcc.NetTestHTTP.util.MyCommonUtil;
import com.bear.cmcc.NetTestHTTP.util.NetTestUtil;
import com.bear.cmcc.NetTestHTTP.util.TimeUtil;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

public class HttpTestExpListAdapter extends BaseExpandableListAdapter {
    private LayoutInflater layoutInflater;

    private Context mContext;

    private HttpTestActivity mActivity;

    private int mGroupIdx;

    private List<HttpTestRoundInfoBean> mGroups;

    private List<List<Map<String, Object>>> mChilds;

    private NetTestSpeedBean mSpeedItem;

    private String show_testing = null;

    public HttpTestExpListAdapter(HttpTestActivity activity, Context mContext,
            List<HttpTestRoundInfoBean> groupList, List<List<Map<String, Object>>> childList,
            NetTestSpeedBean speed) {
        this.mActivity = activity;
        this.mContext = mContext;
        this.mGroups = groupList;
        this.mChilds = childList;
        layoutInflater = LayoutInflater.from(mContext);
        this.mSpeedItem = speed;
        show_testing = mContext.getString(R.string.show_testing);
    }

    public Object getChild(int groupPosition, int childPosition) {
        try {
            if (mChilds == null || mChilds.size() <= 0) {
                return null;
            }
            return mChilds.get(groupPosition).get(childPosition);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {

        ChildHolder holder;

        try {
            if (childPosition == 0) {
                // if (convertView == null)
                {
                    holder = new ChildHolder();
                    convertView = layoutInflater.inflate(R.layout.item_nettest_childs_title, null);
                    holder.tvNetTypeName = (TextView) convertView.findViewById(R.id.netType);
                    holder.tvSeq = (TextView) convertView.findViewById(R.id.testSeq);
                    // holder.tvLacValue = (TextView)
                    // convertView.findViewById(R.id.lac);
                    holder.tvCiValue = (TextView) convertView.findViewById(R.id.ciSpeed);
                    holder.tvElecValue = (TextView) convertView.findViewById(R.id.elec);
                    holder.tvSignalValue = (TextView) convertView.findViewById(R.id.signal);

                    holder.groupIdx = groupPosition;
                    holder.childIdx = childPosition;
                    convertView.setTag(holder);
                    convertView.setTag(R.string.group_item_tag, -1);
                    
                }
                // else {
                // holder = (ChildHolder) convertView.getTag();
                // }

            } else {
                // if (convertView == null)
                {
                    holder = new ChildHolder();
                    convertView = layoutInflater.inflate(R.layout.item_nettest_childs, null);
                    holder.imgNetTypeIcon = (ImageView) convertView.findViewById(R.id.netType_icon);
                    holder.tvNetTypeName = (TextView) convertView.findViewById(R.id.netType);
                    holder.tvSeq = (TextView) convertView.findViewById(R.id.testSeq);
                    // holder.tvLacValue = (TextView)
                    // convertView.findViewById(R.id.lac_value);
                    holder.tvCiValue = (TextView) convertView.findViewById(R.id.ciSpeed_value);
                    holder.tvElecValue = (TextView) convertView.findViewById(R.id.elec_value);
                    holder.tvSignalValue = (TextView) convertView.findViewById(R.id.signal_value);

                    holder.groupIdx = groupPosition;
                    holder.childIdx = childPosition;
                    convertView.setTag(holder);
                    convertView.setTag(R.layout.item_nettest_childs, -1);
                }
                // else {
                // holder = (ChildHolder) convertView.getTag();
                // }
            }

            convertView.setTag(R.string.group_item_pid, mGroups.get(groupPosition).getPID());
            
            holder.tvSeq.setText(mChilds.get(groupPosition).get(childPosition).get("number")
                    .toString());
            String type = mChilds.get(groupPosition).get(childPosition).get("type").toString();
            holder.tvNetTypeName.setText(mChilds.get(groupPosition).get(childPosition).get("type")
                    .toString());
            if (type != null && type.equalsIgnoreCase("wlan")) {
                if (holder.imgNetTypeIcon != null) {
                    holder.imgNetTypeIcon.setImageResource(R.drawable.edge_down);
                }
            } else {
                if (holder.imgNetTypeIcon != null) {
                    holder.imgNetTypeIcon.setImageResource(R.drawable.edge_down);
                }
            }

            // holder.tvLacValue.setText(mChilds.get(groupPosition).get(childPosition).get("lac")
            // .toString());
            holder.tvCiValue.setText((mChilds.get(groupPosition).get(childPosition).get("ci")
                    .toString()));
            holder.tvElecValue.setText(mChilds.get(groupPosition).get(childPosition).get("elec")
                    .toString());
            holder.tvSignalValue.setText(mChilds.get(groupPosition).get(childPosition)
                    .get("signal").toString());

            if (childPosition % 2 == 0) {
                convertView.setBackgroundResource(R.drawable.bg_flutter_list);
            } else {
                convertView.setBackgroundResource(R.drawable.black_background);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        try {
            if (mChilds == null || mChilds.size() <= 0) {
                return 0;
            }
            return mChilds.get(groupPosition).size();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        try {
            if (mGroups == null || mGroups.size() <= 0) {
                return 0;
            }
            return mGroups.get(groupPosition);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int getGroupCount() {
        if (mGroups == null) {
            return 0;
        }
        return mGroups.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
        try {
            mGroupIdx = groupPosition;

            GroupHolder holder;
            holder = new GroupHolder();
            convertView = layoutInflater.inflate(R.layout.item_nettest_groups, null);
            holder.showData = (LinearLayout) convertView.findViewById(R.id.lidata);
            holder.showData1 = (LinearLayout) convertView.findViewById(R.id.dataReport);
            holder.tvTempValue = (TextView) convertView.findViewById(R.id.tvTempValue);
            holder.btnReport = (ImageView) convertView.findViewById(R.id.btnReport);
            holder.tvAveValue = (TextView) convertView.findViewById(R.id.tvSpeedValue);
            holder.tvWavValue = (TextView) convertView.findViewById(R.id.tvWavValue);
            holder.tvVillage = (TextView) convertView.findViewById(R.id.tvVillageValue);
            holder.tvTime = (TextView) convertView.findViewById(R.id.tvTimeValue);
            holder.rightIcon = (ImageView) convertView.findViewById(R.id.ivArrow);

            holder.btnDelete = (Button) convertView.findViewById(R.id.btnDel);

            holder.groupIdx = groupPosition;
            convertView.setTag(holder);
            convertView.setTag(R.string.group_item_tag, 0);

            if (groupPosition == 0 && mSpeedItem != null && (mSpeedItem.getShow() == true)) {
                holder.btnReport.setImageResource(R.drawable.report_write_ico);
                holder.tvTempValue.setText(String.format(show_testing, mSpeedItem.getMax(),
                        mSpeedItem.getMin(), mSpeedItem.getAve()));
                holder.tvTempValue.setTextSize(16);
                holder.showData.setVisibility(View.GONE);
                holder.showData1.setVisibility(View.GONE);
                holder.tvTempValue.setVisibility(View.VISIBLE);

            } else {
                holder.btnReport.setImageResource(R.drawable.report_ico);
                holder.showData.setVisibility(View.VISIBLE);
                holder.showData1.setVisibility(View.VISIBLE);
                holder.tvTempValue.setVisibility(View.GONE);
            }
            
            convertView.setTag(R.string.group_item_pid, mGroups.get(groupPosition).getPID());
            holder.tvTime.setText(TimeUtil.formatTime(mGroups.get(groupPosition).getStartTime())+ " "+mContext.getResources().getString(R.string.test_consumer_time) +mGroups.get(groupPosition).getConsumeTime());
            // holder.tvWavValue.setText(NetTestUtil.getWaveShakeStrong(mGroups.get(groupPosition)
            // .getMaxSpeed(), mGroups.get(groupPosition).getMinSpeed()));
            holder.tvWavValue.setText(NetTestUtil.getWaveShakeStrong(mContext,
                    mGroups.get(groupPosition).getPID()));

            holder.tvAveValue.setText(MyCommonUtil.getFormatString((mGroups.get(groupPosition)
                    .getAverageSpeed())) + "kb/s");

            if (!isExpanded) {
                holder.rightIcon.setImageResource(R.drawable.bigcirclearrow_bak);
            } else {
                holder.rightIcon.setImageBitmap(Rotato90());
            }

            if (mGroups.get(groupPosition).getVillageName() == null) {
                holder.tvVillage.setText(mGroups.get(groupPosition).getVillageName());
            }

            holder.btnReport.setTag(mGroups.get(groupPosition));

            holder.btnReport.setOnClickListener(new MyOnClickListener(mGroups.get(groupPosition)
                    .getPID()));
            // }

            convertView.setTag(R.string.group_item_pid, mGroups.get(groupPosition).getPID());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    private Bitmap Rotato90() {
        Bitmap bit = BitmapFactory.decodeResource(mContext.getResources(),
                R.drawable.bigcirclearrow_bak);// 读取资源里德图片
        // 将图片设置为宽100，高200，在这儿就可以实现图片的大小缩放
        Bitmap resize = Bitmap.createScaledBitmap(bit, bit.getWidth(), bit.getHeight(), true);
        Matrix m = new Matrix();
        int width = resize.getWidth();
        int height = resize.getHeight();
        m.setRotate(90); // 逆时针旋转15度
        // 做好旋转与大小之后，重新创建位图，0-width宽度上显示的区域，高度类似
        Bitmap b = Bitmap.createBitmap(resize, 0, 0, width, height, m, true);
        return b;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public final class GroupSpeedHolder {

        public TextView tvAveValue;

        public TextView tvMaxValue;

        public TextView tvMinValue;

        public int groupIdx;
    }

    public final class GroupHolder {

        public LinearLayout showData;

        public LinearLayout showData1;

        public TextView tvTempValue;

        public ImageView btnReport;

        public TextView tvAveValue;

        public TextView tvWavValue;

        public TextView tvVillage;

        public TextView tvTime;

        public ImageView rightIcon;

        public Button btnDelete;

        public int groupIdx;
    }

    public final class ChildHolder {

        public TextView tvSeq;

        public ImageView imgNetTypeIcon;

        public TextView tvNetTypeName;

        // public TextView tvLacValue;

        public TextView tvCiValue;

        public TextView tvElecValue;

        public TextView tvSignalValue;

        public int groupIdx;

        public int childIdx;
    }

    class MyOnClickListener implements OnClickListener {

        private int pid = 0;

        public MyOnClickListener(int setPid) {
            pid = setPid;
        }

        @Override
        public void onClick(View v) {
            mActivity.reportGenerate(pid);
        }

    }

    class DeleteOnClickListener implements OnClickListener {

        private int pid = 0;

        public DeleteOnClickListener(int setPid) {
            pid = setPid;
        }

        @Override
        public void onClick(View v) {
            mActivity.deleteRound(pid);
        }

    }

}
